package com.md.mymusic.adapter

import android.content.Context
import android.opengl.Visibility
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.md.mymusic.R
import com.md.mymusic.WrapperAlbum

/**
 * @author liyue
 * create 2021/4/5
 * desc 自建专辑和收藏专辑列表adapter
 */
class CreateAlbumAdapter(context: Context, isMine:Boolean,albumList:List<WrapperAlbum>) :RecyclerView.Adapter<CreateAlbumAdapter.AlbumViewHolder>(){

    private var mContext:Context = context
    private var albumList:List<WrapperAlbum> =albumList
    private var isMine= isMine

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlbumViewHolder {
        var view = LayoutInflater.from(mContext).inflate(R.layout.adapter_self_album,parent,false)
        return AlbumViewHolder(view)
    }

    override fun getItemCount(): Int {
       return albumList.size
    }

    override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) {
        var curAlbum = albumList[position]
        holder.tvTitle?.text= curAlbum.album.name
        holder.tvTotalSize?.apply {
                visibility = View.VISIBLE
                text= curAlbum.totalCount.toString()
        }
        holder.cltAlbumItem?.setOnClickListener {
            var albumId= (curAlbum.album.id).toInt()
            ARouter.getInstance()
                    .build("/selfcreate/album")
                    .withString("SELF_CREATE_TITLE", curAlbum.album.name)
                    .withInt("CREATED_ALBUM_ID",albumId)
                    .withBoolean("IS_MINE_MINE",isMine)
                    .navigation()
        }
        Glide.with(mContext)
                .load(curAlbum.album.coverUrl)
                .placeholder(com.md.album.R.drawable.selfalbum_cover_default)
                .into(holder.ivCover!!)
    }

     fun setData(albumList:List<WrapperAlbum>){
        this.albumList= albumList
        notifyDataSetChanged()
    }

    class AlbumViewHolder: RecyclerView.ViewHolder{
        var cltAlbumItem:ConstraintLayout?=null
        var ivCover: ImageView?= null
        var tvTitle: TextView?= null
        var tvTotalSize: TextView?= null
        constructor(itemView:View):super(itemView){
            cltAlbumItem = itemView.findViewById(R.id.cltAlbumItem)
            ivCover = itemView.findViewById(R.id.ivCover)
            tvTitle = itemView.findViewById(R.id.tvTitle)
            tvTotalSize = itemView.findViewById(R.id.tvTotalSize)
        }
    }
}